<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--数据表格css样式-->
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="searchBrand" class="demo-form-inline">

        <el-form-item label="当前状态">
            <el-select v-model="searchBrand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="searchBrand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="searchBrand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="searchSubmit()">查询</el-button>
            <el-button type="info" @click="searchBrand={}, selectAll()">重置</el-button>
        </el-form-item>
    </el-form>

    <!--批量删除和新增按钮-->
    <el-row>
        <el-button type="danger" plain @click="deleteBrands()">批量删除</el-button>
        <el-button type="primary" plain @click="addBrand={}, dialogVisible = true">新增</el-button>
    </el-row>

    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible"
            width="30%"
    >
        <el-form ref="form" :model="addBrand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="addBrand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="addBrand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="addBrand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="addBrand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="addBrand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addSubmit()">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--修改数据对话框表单-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="updateDialogVisible"
            width="30%"
    >
        <el-form ref="form" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="updateBrand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="updateBrand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="updateBrand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="updateBrand.description"></el-input>
            </el-form-item>

            <!--单选按钮-->
            <el-form-item label="状态">
                <el-switch v-model="updateBrand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="updateSubmit()">提交</el-button>
                <el-button @click="updateDialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--数据表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <!--复选框-->
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <!--自定义序号-->
            <el-table-column
                    label="序号"
                    type="index"
                    :index="indexData"
                    width="50">
            </el-table-column>
            <!--表头-->
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <!--在实体类定义状态逻辑视图方法getStatusStr()-->
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
                <!--<template slot-scope="scope">
                    &lt;!&ndash;scope.row:表示当前行&ndash;&gt;
                    {{scope.row.status == '1' ? '启用' : '禁用'}}
                </template>-->
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作">
                <!--利用scope.row获取本行数据对象-->
                <template slot-scope="scope">
                    <!--修改删除按钮-->
                    <el-row>
                        <el-button type="primary" @click="updateBtn(scope.row)">修改</el-button>
                        <el-button type="danger" @click="delSubmit(scope.row)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>

<!--引入js和css-->
<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    /*Vue核心对象*/
    new Vue({
        el: "#app",
        methods: {
            //表格布局方法
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            //页面加载时 --> 查询所有的方法(分页查询/条件查询)
            selectAll() {
                //普通查询
                let _this = this;
                axios.post("http://localhost:8080/brandCase/brand/selectAll").then(function (resp){
                    _this.tableData = resp.data;
                });
                //分页查询
                /*axios.post("http://localhost:8080/brandCase/brand/selectByPage?currentPage="
                    +this.currentPage+"&pageSize="+this.pageSize)
                   .then(resp => {
                       this.tableData = resp.data.rows;
                       this.totalCount = resp.data.totalCount;
                   });*/
                //条件查询
                axios.post("http://localhost:8080/brandCase/brand/selectByConditionAndPage?currentPage="
                    + this.currentPage + "&pageSize=" + this.pageSize, this.searchBrand)
                    .then(resp => {
                        this.tableData = resp.data.rows;
                        this.totalCount = resp.data.totalCount;
                        this.indexData = (this.currentPage - 1) * this.pageSize + 1;
                    });
            },
            // 添加数据【提交按钮】 --> 点击事件
            addSubmit() {
                axios.post("http://localhost:8080/brandCase/brand/add", this.addBrand)
                    .then(resp => {
                        if (resp.data) {
                            this.dialogVisible = false;
                            this.searchBrand = {};
                            this.currentPage = 1;
                            this.selectAll();
                            //alert("添加成功！");
                            this.$message({
                                message: '添加成功！',
                                type: 'success'
                            });
                        }
                    });
            },
            //【删除按钮】 --> 点击事件
            delSubmit(row) {
                this.delBrand = row;
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post("http://localhost:8080/brandCase/brand/delete", this.delBrand)
                        .then(resp => {
                            if (resp.data) {
                                //alert("删除成功！");
                                this.$message({
                                    message: '删除成功！',
                                    type: 'success'
                                });
                                this.selectAll();
                            }
                        });
                })
            },
            //【修改按钮】 --> 点击事件
            updateBtn(row) {
                //console.log(updateBrand);
                this.updateBrand.id = row.id;
                this.updateBrand.brandName = row.brandName;
                this.updateBrand.companyName = row.companyName;
                this.updateBrand.ordered = row.ordered;
                this.updateBrand.description = row.description;
                this.updateBrand.status = row.status + '';
                this.updateDialogVisible = true;
            },
            //修改对话框【提交按钮】 --> 点击事件
            updateSubmit() {
                axios.post("http://localhost:8080/brandCase/brand/update", this.updateBrand)
                    .then(resp => {
                        if (resp.data) {
                            //alert("修改成功！");
                            this.$message({
                                message: '修改成功！',
                                type: 'success'
                            });
                            this.updateDialogVisible = false;
                            this.selectAll();
                        }
                    });
            },
            // 【复选框】 --> 选中后触发事件
            handleSelectionChange(val) {
                this.multipleSelection = val;
                //console.log(this.multipleSelection.length)
            },
            //【批量删除按钮】 --> 点击事件
            deleteBrands() {
                if (this.multipleSelection.length > 0) {
                    this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //将复选框选中的对象的所有id放到selectIds[]数组中
                        for (let i = 0; i < this.multipleSelection.length; i++) {
                            let brand = this.multipleSelection[i];
                            this.selectIds[i] = brand.id;
                        }
                        //将存储id的数组作为请求参数访问服务器
                        axios.post("http://localhost:8080/brandCase/brand/deleteByIds", this.selectIds)
                            .then(resp => {
                                if (resp.data) {
                                    //alert("删除成功！");
                                    this.selectAll();
                                    this.$message({
                                        message: '删除成功！',
                                        type: 'success'
                                    });
                                }
                            });
                    })
                }
            },
            //【5条/页】 --> 改变时触发事件
            handleSizeChange(val) {
                //console.log(val);
                this.pageSize = val;
                this.selectAll();
            },
            //【当前页】 --> 改变时触发事件
            handleCurrentChange(val) {
                //console.log(val);
                this.currentPage = val;
                this.selectAll();
            },
            // 搜索框表单【查询按钮】 --> 点击事件
            searchSubmit() {
                //console.log(this.searchBrand);
                //条件查询
                this.currentPage = 1;
                axios.post("http://localhost:8080/brandCase/brand/selectByConditionAndPage?currentPage="
                    + this.currentPage + "&pageSize=" + this.pageSize, this.searchBrand)
                    .then(resp => {
                        this.tableData = resp.data.rows;
                        this.totalCount = resp.data.totalCount;
                    });
            },
        },
        data() {
            return {
                // 表格数据
                tableData: "",
                // 添加品牌数据模型
                addBrand: {
                    id: "",
                    brandName: '',
                    companyName: '',
                    ordered: "",
                    description: "",
                    status: ''
                },
                // 修改品牌数据模型
                updateBrand: {
                    id: "",
                    brandName: '',
                    companyName: '',
                    ordered: "",
                    description: "",
                    status: ''
                },
                // 删除品牌数据模型
                delBrand: {
                    id: "",
                    brandName: '',
                    companyName: '',
                    ordered: "",
                    description: "",
                    status: ''
                },
                // 搜索表单品牌数据模型
                searchBrand: {
                    id: "",
                    brandName: '',
                    companyName: '',
                    ordered: "",
                    description: "",
                    status: ''
                },
                // 添加数据【对话框】是否展示的标记
                dialogVisible: false,
                // 修改数据【对话框】是否展示的标记
                updateDialogVisible: false,
                // 【复选框】选中的数据模型
                multipleSelection: [],
                //【复选框】选中的数据的id集合数据模型
                selectIds: [],
                //分页查询 --> 当前页码
                currentPage: 1,
                //分页查询 --> 每页条数
                pageSize: 5,
                //分页查询 --> 数据条数
                totalCount: 200,
                // 自定义【序号】数据模型 -->
                // this.indexData = (this.currentPage - 1) * this.pageSize + 1;
                indexData: 1
            }
        },
        //初始化操作 --> 页面加载完成时执行
        mounted() {
            this.selectAll();
        }
    })
</script>
</body>
</html>